import { promises } from "fs";
import fsextra from "fs-extra";
import path from "path";

import { __dirname } from "./index";
import { minifyhtml } from "./minifyhtml";

export async function savemddatahtml(
    url: string,
    pagepath: string,
    cachemain: Map<string, string>
) {
    const key = url;
    //@ts-ignore
    let filename = btoa(key) + ".html";
    let mdfilepath = path.join(pagepath, filename);

    const markdown = cachemain.get(url);
    if (!markdown) {
        throw new Error("markdown not found");
    }
    const mainmini =
        process.env.NODE_ENV === "development"
            ? markdown
            : await minifyhtml(markdown);
    const serilizedtext = mainmini;

    await fsextra.ensureFile(mdfilepath);
    await promises.writeFile(mdfilepath, serilizedtext);
}
